<!DOCTYPE html>
<script src="/js/src/click.js"></script>
<html  lang="zh">
<head>
    <meta charset="utf-8" />

<meta name="generator" content="Hexo 4.2.0" />

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />

<title>如何在Python中使用Linux-epoll - ccf19881030的博客</title>


    <meta name="description" content="如何在Python中使用Linux epoll内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码介绍从2.6版开始，Python包含用于访问Linux epoll库的API。本文使用Python3示例简要演示API。阻塞套接字编程示例示例1是一个简单的Python服务器，它在8080端口上侦听HTTP请求，将其打印到控制台，然">
<meta property="og:type" content="article">
<meta property="og:title" content="如何在Python中使用Linux-epoll">
<meta property="og:url" content="http://havealex.cn/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll/index.html">
<meta property="og:site_name" content="ccf19881030的博客">
<meta property="og:description" content="如何在Python中使用Linux epoll内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码介绍从2.6版开始，Python包含用于访问Linux epoll库的API。本文使用Python3示例简要演示API。阻塞套接字编程示例示例1是一个简单的Python服务器，它在8080端口上侦听HTTP请求，将其打印到控制台，然">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://havealex.cn/images/og_image.png">
<meta property="article:published_time" content="2020-03-02T07:24:16.869Z">
<meta property="article:modified_time" content="2020-03-02T07:24:16.869Z">
<meta property="article:author" content="ccf19881030">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="SpringBoot">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="PHP">
<meta property="article:tag" content="C#">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://havealex.cn/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/atom-one-dark.css">


    
    
<style>body>.footer,body>.navbar,body>.section{opacity:0}</style>

    
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">

    
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">

    
    
    
    
<link rel="stylesheet" href="/css/back-to-top.css">

    
    
    
    
    
    
    
    <link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
    
    <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    


<link rel="stylesheet" href="/css/style.css">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="如何在Python中使用Linux-epoll" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">首页</a>
                
                <a class="navbar-item"
                href="/archives">归档</a>
                
                <a class="navbar-item"
                href="/categories">分类</a>
                
                <a class="navbar-item"
                href="/tags">标签</a>
                
                <a class="navbar-item"
                href="/about">关于</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    <a class="navbar-item" target="_blank" rel="noopener" title="Download on GitHub" href="https://github.com/ccf19881030">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                
                <a class="navbar-item search" title="搜索" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-6-widescreen has-order-2 column-main">
<div class="card">
    
    <div class="card-content article ">
		<span id="busuanzi_container_page_pv">
			本文总阅读量<span id="busuanzi_value_page_pv"></span>次
		</span>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2020-03-02T07:24:16.869Z">2020-03-02</time>
                
                
                <span class="level-item has-text-grey">
                    
                    
                    31 分钟 读完 (大约 4652 个字)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span>次访问
                </span>
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                如何在Python中使用Linux-epoll
            
        </h1>
        <div class="content">
            <h1 id="如何在Python中使用Linux-epoll"><a href="#如何在Python中使用Linux-epoll" class="headerlink" title="如何在Python中使用Linux epoll"></a>如何在Python中使用Linux epoll</h1><h2 id="内容"><a href="#内容" class="headerlink" title="内容"></a>内容</h2><ul>
<li>介绍</li>
<li>阻塞套接字编程示例</li>
<li>异步套接字和Linux epoll的好处</li>
<li>epoll的异步套接字编程示例</li>
<li>性能考量</li>
<li>源代码<h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3>从2.6版开始，Python包含用于访问Linux epoll库的API。本文使用Python3示例简要演示API。<h3 id="阻塞套接字编程示例"><a href="#阻塞套接字编程示例" class="headerlink" title="阻塞套接字编程示例"></a>阻塞套接字编程示例</h3>示例1是一个简单的Python服务器，它在8080端口上侦听HTTP请求，将其打印到控制台，然后将HTTP响应发送回客户端。</li>
<li>第9行：创建服务套接字</li>
<li>第10行：即使最近另一个程序正在同一端口上侦听，也允许在第11行中使用bind（）。否则，直到使用该端口的上一个程序完成一两分钟后，该程序才能运行。</li>
<li>第11行：将服务器套接字绑定到此计算机上所有可用IPv4地址的端口8080。</li>
<li>第12行：告诉服务器套接字开始接受来自客户端的传入连接。</li>
<li>第14行：程序将在此处停止，直到接收到连接为止。发生这种情况时，服务器套接字将在此计算机上创建一个用于与客户端通信的新套接字。这个新的套接字由accept（）调用返回的clientconnection对象表示。地址对象指示连接另一端的IP地址和端口号。</li>
<li>第15-17行：组装客户端正在传输的数据，直到传输了完整的HTTP请求为止。 <a href="http://www.jmarshall.com/easy/http/">HTTP简易描述</a>中介绍了HTTP协议。</li>
<li>第18行：将请求打印到控制台，以验证操作是否正确。</li>
<li>第19行：将响应发送给客户端。</li>
<li>第20-22行：关闭与客户端以及侦听服务器套接字的连接。<br>官方的<a href="http://docs.python.org/3.0/howto/sockets.html">HOWTO</a>对使用Python的套接字编程有更详细的描述。<br>// Example 1 (All examples use Python 3)<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">connectiontoclient, address = serversocket.accept()</span><br><span class="line">request = <span class="string">b''</span></span><br><span class="line"><span class="keyword">while</span> EOL1 <span class="keyword">not</span> <span class="keyword">in</span> request <span class="keyword">and</span> EOL2 <span class="keyword">not</span> <span class="keyword">in</span> request:</span><br><span class="line">   request += connectiontoclient.recv(<span class="number">1024</span>)</span><br><span class="line">print(request.decode())</span><br><span class="line">connectiontoclient.send(response)</span><br><span class="line">connectiontoclient.close()</span><br><span class="line"></span><br><span class="line">serversocket.close()</span><br></pre></td></tr></table></figure>
示例2在第15行中添加了一个循环，以重复处理客户端连接，直到被用户中断（例如，键盘中断）。 这更清楚地说明了服务器套接字从未用于与客户端交换数据。 而是，它接受来自客户端的连接，然后在服务器计算机上创建用于与客户端通信的新套接字。</li>
</ul>
<p>第23-24行的finally语句块可确保侦听服务器套接字始终关闭，即使发生异常也是如此。<br>// Example 2</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">      connectiontoclient, address = serversocket.accept()</span><br><span class="line">      request = <span class="string">b''</span></span><br><span class="line">      <span class="keyword">while</span> EOL1 <span class="keyword">not</span> <span class="keyword">in</span> request <span class="keyword">and</span> EOL2 <span class="keyword">not</span> <span class="keyword">in</span> request:</span><br><span class="line">          request += connectiontoclient.recv(<span class="number">1024</span>)</span><br><span class="line">      print(<span class="string">'-'</span>*<span class="number">40</span> + <span class="string">'\n'</span> + request.decode()[:<span class="number">-2</span>])</span><br><span class="line">      connectiontoclient.send(response)</span><br><span class="line">      connectiontoclient.close()</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">   serversocket.close()</span><br></pre></td></tr></table></figure>
<h3 id="异步套接字和Linux-epoll的好处"><a href="#异步套接字和Linux-epoll的好处" class="headerlink" title="异步套接字和Linux epoll的好处"></a>异步套接字和Linux epoll的好处</h3><p>示例2中显示的套接字称为阻塞套接字，因为Python程序会停止运行直到事件发生。第16行中的accept（）调用将阻塞，直到从客户端接收到连接为止。第19行中的recv（）调用将阻塞，直到从客户端接收到数据为止（或直到​​没有其他数据要接收为止）。第21行中的send（）调用将阻塞，直到Linux将所有返回给客户端的数据排队等待准备传输。</p>
<p>当程序使用阻塞套接字时，它通常使用一个线程（甚至是专用进程）在每个套接字上进行通信。主程序线程将包含侦听服务器套接字，该套接字接受来自客户端的传入连接。它将一次接受这些连接，将新创建的套接字传递给一个单独的线程，然后该线程将与客户端进行交互。因为这些线程中的每一个仅与一个客户端通信，所以任何阻塞都不会阻止其他线程执行其各自的任务。</p>
<p>将阻塞套接字与多个线程一起使用会导致代码简单明了，但存在<a href="http://www.virtualdub.org/blog/pivot/entry.php?id=62">许多缺点</a>。 共享资源时，可能难以确保线程适当协作。 在只有一个CPU的计算机上，这种编程风格的效率可能较低。</p>
<p><a href="http://www.kegel.com/c10k.html">C10K问题</a>讨论了用于处理多个并发套接字的一些替代方法，例如异步套接字的使用。 这些套接字在某些事件发生之前不会阻塞。 而是，程序在异步套接字上执行一个操作，并立即通知该操作成功还是失败。 该信息使程序可以决定如何进行。 由于异步套接字是非阻塞的，因此不需要多个执行线程。 所有工作都可以在单个线程中完成。 这种单线程方法有其自身的挑战，但对于许多程序来说可能是一个不错的选择。 它也可以与多线程方法结合使用：使用单线程的异步套接字可以用于服务器的网络组件，而线程可以用于访问其他阻塞资源，例如 数据库。<br>Linux有许多用于管理异步套接字的机制，其中三种由Python select，poll和epoll API公开。 epoll和poll比select更好，因为Python程序不必检查每个套接字中是否有感兴趣的事件。 相反，它可以依靠操作系统来告诉它哪些套接字可能发生这些事件。 epoll比poll更好，因为它不需要操作系统每次在Python程序查询时都检查所有套接字中是否有感兴趣的事件。 相反，Linux会跟踪这些事件的发生情况，并在由Python查询时返回一个列表。 <a href="http://lse.sourceforge.net/epoll/index.html">这些图</a>显示了使用数千个并行套接字连接时epoll的优势。</p>
<h3 id="epoll的异步套接字编程示例"><a href="#epoll的异步套接字编程示例" class="headerlink" title="epoll的异步套接字编程示例"></a>epoll的异步套接字编程示例</h3><p>使用epoll的程序通常按以下顺序执行操作：</p>
<ol>
<li>创建一个epoll对象</li>
<li>告诉epoll对象监视特定套接字上的特定事件</li>
<li>询问epoll对象，自上次查询以来，哪些套接字可能已经发生了指定的事件</li>
<li>在这些套接字上执行一些操作</li>
<li>告诉epoll对象修改要监视的套接字和/或事件的列表</li>
<li>重复步骤3至5，直到完成</li>
<li>销毁epoll对象<br>示例3复制了示例2的功能然而使用了异步套接字。 该程序更加复杂，因为单个线程正在与多个客户端进行通信交互。</li>
</ol>
<ul>
<li>第1行：select模块包含epoll功能。</li>
<li>第13行：由于默认情况下套接字是阻塞的，因此使用非阻塞（异步）模式是必需的。</li>
<li>第15行：创建一个epoll对象。</li>
<li>第16行：对服务器套接字上的读取事件感兴趣。只要服务器套接字接受套接字连接，就会发生读取事件。</li>
<li>第19行：连接字典将文件描述符（整数）映射到它们相应的网络连接对象。</li>
<li>第21行：查询epoll对象以查明是否可能发生了感兴趣的事件。参数“ 1”表示我们愿意等待一秒钟以等待此类事件的发生。如果在此查询之前 发生了任何感兴趣的事件，该查询将立即返回并列出这些事件。</li>
<li>第22行：事件以（文件号，事件代码）元组的序列返回。 fileno是文件描述符的同义词，并且始终是整数。</li>
<li>第23行：如果套接字服务器上发生读取事件，则可能已经创建了新的套接字连接。</li>
<li>第25行：将新套接字设置为非阻塞模式。</li>
<li>第26行：对新套接字的读取（EPOLLIN）事件感兴趣。</li>
<li>第31行：如果发生读取事件，则读取从客户端发送的新数据。</li>
<li>第33行：收到完整的请求后，然后取消注册对读取事件的兴趣并注册对写入（EPOLLOUT）事件的兴趣。当可以将响应数据发送回客户端时，将发生写事件。</li>
<li>第34行：打印完整的请求，表明尽管与客户的通信是交错的，但这些数据可以作为整体消息进行组合和处理。</li>
<li>第35行：如果客户端套接字上发生了写入事件，则它可以接受新数据以发送到客户端。</li>
<li>第36-38行：一次发送一次响应数据，直到将完整的响应传递到操作系统进行传输为止。</li>
<li>第39行：发送完完整的响应后，请停止对进一步的读取或写入事件感兴趣。</li>
<li>第40行：如果显式关闭了连接，则套接字关闭是可选的。此示例程序使用它来使客户端首先关闭。 shutdown调用通知客户端套接字不应再发送或接收任何数据，并且将使行为良好的客户端从其末端关闭套接字连接。</li>
<li>第41行：HUP（挂断）事件表示客户端套接字已断开连接（即已关闭），因此该端也已关闭。无需注册对HUP事件的兴趣。它们始终显示在向epoll对象注册的套接字上。</li>
<li>第42行：取消对此套接字连接的兴趣。</li>
<li>43行：关闭套接字连接。</li>
<li>第18-45行：包含了try-catch块，因为示例程序很可能会被KeyboardInterrupt异常中断</li>
<li>第46-48行：不需要关闭打开的套接字连接，因为Python会在程序终止时关闭它们。出于良好的形式将它们包括在内。<br>// Example 3<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket, select</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line">serversocket.setblocking(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">epoll = select.epoll()</span><br><span class="line">epoll.register(serversocket.fileno(), select.EPOLLIN)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   connections = &#123;&#125;; requests = &#123;&#125;; responses = &#123;&#125;</span><br><span class="line">   <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">      events = epoll.poll(<span class="number">1</span>)</span><br><span class="line">      <span class="keyword">for</span> fileno, event <span class="keyword">in</span> events:</span><br><span class="line">         <span class="keyword">if</span> fileno == serversocket.fileno():</span><br><span class="line">            connection, address = serversocket.accept()</span><br><span class="line">            connection.setblocking(<span class="number">0</span>)</span><br><span class="line">            epoll.register(connection.fileno(), select.EPOLLIN)</span><br><span class="line">            connections[connection.fileno()] = connection</span><br><span class="line">            requests[connection.fileno()] = <span class="string">b''</span></span><br><span class="line">            responses[connection.fileno()] = response</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLIN:</span><br><span class="line">            requests[fileno] += connections[fileno].recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">if</span> EOL1 <span class="keyword">in</span> requests[fileno] <span class="keyword">or</span> EOL2 <span class="keyword">in</span> requests[fileno]:</span><br><span class="line">               epoll.modify(fileno, select.EPOLLOUT)</span><br><span class="line">               print(<span class="string">'-'</span>*<span class="number">40</span> + <span class="string">'\n'</span> + requests[fileno].decode()[:<span class="number">-2</span>])</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLOUT:</span><br><span class="line">            byteswritten = connections[fileno].send(responses[fileno])</span><br><span class="line">            responses[fileno] = responses[fileno][byteswritten:]</span><br><span class="line">            <span class="keyword">if</span> len(responses[fileno]) == <span class="number">0</span>:</span><br><span class="line">               epoll.modify(fileno, <span class="number">0</span>)</span><br><span class="line">               connections[fileno].shutdown(socket.SHUT_RDWR)</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLHUP:</span><br><span class="line">            epoll.unregister(fileno)</span><br><span class="line">            connections[fileno].close()</span><br><span class="line">            <span class="keyword">del</span> connections[fileno]</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">   epoll.unregister(serversocket.fileno())</span><br><span class="line">   epoll.close()</span><br><span class="line">   serversocket.close()</span><br></pre></td></tr></table></figure>
epoll具有两种操作模式，称为边沿触发和水平触发。在边缘触发的操作模式下，对epoll.poll（）的调用仅在套接字上发生读取或写入事件之后，才在该套接字上返回一个事件。调用程序必须处理与该事件相关的所有数据，而在后续对epoll.poll（）的调用中没有进一步的通知。当来自特定事件的数据耗尽时，在套接字上进行其他操作的尝试将导致异常。相反，在级别触发的操作模式下，重复调用epoll.poll（）将导致重复关注感兴趣的事件，直到处理完与该事件相关的所有数据为止。电平触发模式下通常不会发生异常。</li>
</ul>
<p>例如，假设服务器套接字已向epoll对象注册以进行读取事件。在边缘触发模式下，程序将需要接受（）新的套接字连接，直到出现socket.error异常。而在级别触发的操作模式下，可以进行单个accept（）调用，然后可以再次查询epoll对象以获取服务器套接字上的新事件，该事件表示应进行附加的accept（）调用。</p>
<p>示例3使用了电平触发模式，这是默认的操作模式。示例4演示了如何使用边沿触发模式。在示例4中，第25、36和45行引入了循环，直到发生异常为止（否则，其他所有数据将被处理）。第32、38和48行捕获了预期的套接字异常。最后，第16、28、41和51行添加了EPOLLET掩码，该掩码用于设置边沿触发模式。<br>// Example 4</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> socket, select</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line">serversocket.setblocking(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">epoll = select.epoll()</span><br><span class="line">epoll.register(serversocket.fileno(), select.EPOLLIN | select.EPOLLET)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   connections = &#123;&#125;; requests = &#123;&#125;; responses = &#123;&#125;</span><br><span class="line">   <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">      events = epoll.poll(<span class="number">1</span>)</span><br><span class="line">      <span class="keyword">for</span> fileno, event <span class="keyword">in</span> events:</span><br><span class="line">         <span class="keyword">if</span> fileno == serversocket.fileno():</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">               <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">                  connection, address = serversocket.accept()</span><br><span class="line">                  connection.setblocking(<span class="number">0</span>)</span><br><span class="line">                  epoll.register(connection.fileno(), select.EPOLLIN | select.EPOLLET)</span><br><span class="line">                  connections[connection.fileno()] = connection</span><br><span class="line">                  requests[connection.fileno()] = <span class="string">b''</span></span><br><span class="line">                  responses[connection.fileno()] = response</span><br><span class="line">            <span class="keyword">except</span> socket.error:</span><br><span class="line">               <span class="keyword">pass</span></span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLIN:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">               <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">                  requests[fileno] += connections[fileno].recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">except</span> socket.error:</span><br><span class="line">               <span class="keyword">pass</span></span><br><span class="line">            <span class="keyword">if</span> EOL1 <span class="keyword">in</span> requests[fileno] <span class="keyword">or</span> EOL2 <span class="keyword">in</span> requests[fileno]:</span><br><span class="line">               epoll.modify(fileno, select.EPOLLOUT | select.EPOLLET)</span><br><span class="line">               print(<span class="string">'-'</span>*<span class="number">40</span> + <span class="string">'\n'</span> + requests[fileno].decode()[:<span class="number">-2</span>])</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLOUT:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">               <span class="keyword">while</span> len(responses[fileno]) &gt; <span class="number">0</span>:</span><br><span class="line">                  byteswritten = connections[fileno].send(responses[fileno])</span><br><span class="line">                  responses[fileno] = responses[fileno][byteswritten:]</span><br><span class="line">            <span class="keyword">except</span> socket.error:</span><br><span class="line">               <span class="keyword">pass</span></span><br><span class="line">            <span class="keyword">if</span> len(responses[fileno]) == <span class="number">0</span>:</span><br><span class="line">               epoll.modify(fileno, select.EPOLLET)</span><br><span class="line">               connections[fileno].shutdown(socket.SHUT_RDWR)</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLHUP:</span><br><span class="line">            epoll.unregister(fileno)</span><br><span class="line">            connections[fileno].close()</span><br><span class="line">            <span class="keyword">del</span> connections[fileno]</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">   epoll.unregister(serversocket.fileno())</span><br><span class="line">   epoll.close()</span><br><span class="line">   serversocket.close()</span><br></pre></td></tr></table></figure>
<p>由于它们相似，因此在移植使用选择或轮询机制的应用程序时通常使用级别触发模式，而当程序员不需要或希望操作系统提供尽可能多的帮助时，可以使用边缘触发模式。 在管理事件状态。</p>
<p>除了这两种操作模式外，还可以使用EPOLLONESHOT事件掩码向epoll对象注册套接字。 使用此选项时，已注册事件仅对epoll.poll（）的一次调用有效，此后将其自动从要监视的已注册套接字列表中删除。</p>
<h3 id="性能考量"><a href="#性能考量" class="headerlink" title="性能考量"></a>性能考量</h3><h4 id="监听积压队列大小"><a href="#监听积压队列大小" class="headerlink" title="监听积压队列大小"></a>监听积压队列大小</h4><p>在示例1-4中，第12行显示了对serversocket.listen（）方法的调用。 此方法的参数是侦听积压队列大小。 它告诉操作系统在Python程序接受之前有多少TCP / IP连接要接受并放置在积压队列中。 每次Python程序在服务器套接字上调用accept（）时，都会从队列中删除其中一个连接，并且该插槽可用于另一个传入连接。 如果队列已满，则新的传入连接将被静默忽略，从而导致网络连接客户端不必要的延迟。 生产服务器通常会处理数十个或数百个同时连接，因此1值通常不足。 例如，当使用ab对具有100个并发HTTP 1.0客户端的这些示例程序执行负载测试时，任何小于50的积压值通常会导致性能下降。</p>
<h4 id="TCP选项"><a href="#TCP选项" class="headerlink" title="TCP选项"></a>TCP选项</h4><p><a href="http://baus.net/on-tcp_cork">TCP_CORK</a>选项可用于“填充”消息，直到它们准备好发送为止。 如示例5的第34和40行所示，此选项对于使用HTTP / 1.1流水线的HTTP服务器可能是一个不错的选择。<br>// Example 5</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket, select</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line">serversocket.setblocking(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">epoll = select.epoll()</span><br><span class="line">epoll.register(serversocket.fileno(), select.EPOLLIN)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   connections = &#123;&#125;; requests = &#123;&#125;; responses = &#123;&#125;</span><br><span class="line">   <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">      events = epoll.poll(<span class="number">1</span>)</span><br><span class="line">      <span class="keyword">for</span> fileno, event <span class="keyword">in</span> events:</span><br><span class="line">         <span class="keyword">if</span> fileno == serversocket.fileno():</span><br><span class="line">            connection, address = serversocket.accept()</span><br><span class="line">            connection.setblocking(<span class="number">0</span>)</span><br><span class="line">            epoll.register(connection.fileno(), select.EPOLLIN)</span><br><span class="line">            connections[connection.fileno()] = connection</span><br><span class="line">            requests[connection.fileno()] = <span class="string">b''</span></span><br><span class="line">            responses[connection.fileno()] = response</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLIN:</span><br><span class="line">            requests[fileno] += connections[fileno].recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">if</span> EOL1 <span class="keyword">in</span> requests[fileno] <span class="keyword">or</span> EOL2 <span class="keyword">in</span> requests[fileno]:</span><br><span class="line">               epoll.modify(fileno, select.EPOLLOUT)</span><br><span class="line">               connections[fileno].setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, <span class="number">1</span>)</span><br><span class="line">               print(<span class="string">'-'</span>*<span class="number">40</span> + <span class="string">'\n'</span> + requests[fileno].decode()[:<span class="number">-2</span>])</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLOUT:</span><br><span class="line">            byteswritten = connections[fileno].send(responses[fileno])</span><br><span class="line">            responses[fileno] = responses[fileno][byteswritten:]</span><br><span class="line">            <span class="keyword">if</span> len(responses[fileno]) == <span class="number">0</span>:</span><br><span class="line">               connections[fileno].setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, <span class="number">0</span>)</span><br><span class="line">               epoll.modify(fileno, <span class="number">0</span>)</span><br><span class="line">               connections[fileno].shutdown(socket.SHUT_RDWR)</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLHUP:</span><br><span class="line">            epoll.unregister(fileno)</span><br><span class="line">            connections[fileno].close()</span><br><span class="line">            <span class="keyword">del</span> connections[fileno]</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">   epoll.unregister(serversocket.fileno())</span><br><span class="line">   epoll.close()</span><br><span class="line">   serversocket.close()</span><br></pre></td></tr></table></figure>
<p>另一方面，<a href="http://unixguide.net/network/socketfaq/2.16.shtml">TCP_NODELAY</a>选项可用于告诉操作系统任何传递给socket.send（）的数据都应立即发送给客户端，而不要被操作系统缓冲。 如示例6的第14行所示，此选项可能是用于SSH客户端或其他“实时”应用程序的不错的选择。<br>// Example 6</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> socket, select</span><br><span class="line"></span><br><span class="line">EOL1 = <span class="string">b'\n\n'</span></span><br><span class="line">EOL2 = <span class="string">b'\n\r\n'</span></span><br><span class="line">response  = <span class="string">b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'</span></span><br><span class="line">response += <span class="string">b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'</span></span><br><span class="line">response += <span class="string">b'Hello, world!'</span></span><br><span class="line"></span><br><span class="line">serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="number">1</span>)</span><br><span class="line">serversocket.bind((<span class="string">'0.0.0.0'</span>, <span class="number">8080</span>))</span><br><span class="line">serversocket.listen(<span class="number">1</span>)</span><br><span class="line">serversocket.setblocking(<span class="number">0</span>)</span><br><span class="line">serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">epoll = select.epoll()</span><br><span class="line">epoll.register(serversocket.fileno(), select.EPOLLIN)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   connections = &#123;&#125;; requests = &#123;&#125;; responses = &#123;&#125;</span><br><span class="line">   <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">      events = epoll.poll(<span class="number">1</span>)</span><br><span class="line">      <span class="keyword">for</span> fileno, event <span class="keyword">in</span> events:</span><br><span class="line">         <span class="keyword">if</span> fileno == serversocket.fileno():</span><br><span class="line">            connection, address = serversocket.accept()</span><br><span class="line">            connection.setblocking(<span class="number">0</span>)</span><br><span class="line">            epoll.register(connection.fileno(), select.EPOLLIN)</span><br><span class="line">            connections[connection.fileno()] = connection</span><br><span class="line">            requests[connection.fileno()] = <span class="string">b''</span></span><br><span class="line">            responses[connection.fileno()] = response</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLIN:</span><br><span class="line">            requests[fileno] += connections[fileno].recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">if</span> EOL1 <span class="keyword">in</span> requests[fileno] <span class="keyword">or</span> EOL2 <span class="keyword">in</span> requests[fileno]:</span><br><span class="line">               epoll.modify(fileno, select.EPOLLOUT)</span><br><span class="line">               print(<span class="string">'-'</span>*<span class="number">40</span> + <span class="string">'\n'</span> + requests[fileno].decode()[:<span class="number">-2</span>])</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLOUT:</span><br><span class="line">            byteswritten = connections[fileno].send(responses[fileno])</span><br><span class="line">            responses[fileno] = responses[fileno][byteswritten:]</span><br><span class="line">            <span class="keyword">if</span> len(responses[fileno]) == <span class="number">0</span>:</span><br><span class="line">               epoll.modify(fileno, <span class="number">0</span>)</span><br><span class="line">               connections[fileno].shutdown(socket.SHUT_RDWR)</span><br><span class="line">         <span class="keyword">elif</span> event &amp; select.EPOLLHUP:</span><br><span class="line">            epoll.unregister(fileno)</span><br><span class="line">            connections[fileno].close()</span><br><span class="line">            <span class="keyword">del</span> connections[fileno]</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">   epoll.unregister(serversocket.fileno())</span><br><span class="line">   epoll.close()</span><br><span class="line">   serversocket.close()</span><br></pre></td></tr></table></figure>
<h3 id="源代码"><a href="#源代码" class="headerlink" title="源代码"></a>源代码</h3><p>本页上的示例位于公共领域，可以<a href="http://scotdoyle.com/python-epoll-examples.tar.gz">下载</a>。</p>

        </div>
        
        
        
        <div class="social-share"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css">
<script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script>
        
    </div>
</div>



<div class="card">
    <div class="card-content">
        <h3 class="menu-label has-text-centered">喜欢这篇文章？打赏一下作者吧</h3>
        <div class="buttons is-centered">
            
                
<a class="button is-info donate">
    <span class="icon is-small">
        <i class="fab fa-alipay"></i>
    </span>
    <span>支付宝</span>
    <div class="qrcode"><img src="/images/alipay.png" alt="支付宝"></div>
</a>

                
                
<a class="button is-success donate">
    <span class="icon is-small">
        <i class="fab fa-weixin"></i>
    </span>
    <span>微信</span>
    <div class="qrcode"><img src="/images/weixin.png" alt="微信"></div>
</a>

                
        </div>
    </div>
</div>



<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll-1/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">如何在Python中使用Linux-epoll</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/2020/02/21/QT%E5%BC%80%E6%BA%90%E7%BD%91%E7%AB%99%E5%92%8C%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/">
                <span class="level-item">QT开源网站和相关资料</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>



</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level">
            <div class="level-item has-text-centered" style="flex-shrink: 1">
                <div>
                    
                    <figure class="image is-128x128 has-mb-6">
                        <img class="" src="/images/avatar.png" alt="ccf19881030的博客">
                    </figure>
                    
                    <p class="is-size-4 is-block">
                        ccf19881030的博客
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        武汉加油！
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>中国武汉</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level is-mobile">
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        文章
                    </p>
                    <a href="/archives">
                        <p class="title has-text-weight-normal">
                            7
                        </p>
                    </a>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        分类
                    </p>
                    <a href="/categories">
                        <p class="title has-text-weight-normal">
                            0
                        </p>
                    </a>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        标签
                    </p>
                    <a href="/tags">
                        <p class="title has-text-weight-normal">
                            0
                        </p>
                    </a>
                </div>
            </div>
        </nav>
        
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://github.com/ccf19881030" target="_blank" rel="noopener">
                关注我</a>
        </div>
        
        
        
        <div class="level is-mobile">
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="Csdn" href="https://blog.csdn.net/ccf19881030">
                
                <i class="fab fa-csdn"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="我的跟人网站" href="http://havealex.cn">
                
                <i class="fab fa-havealex"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="Github" href="https://github.com/ccf19881030">
                
                <i class="fab fa-github"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="Facebook" href="https://facebook.com">
                
                <i class="fab fa-facebook"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="Twitter" href="https://twitter.com">
                
                <i class="fab fa-twitter"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="Dribbble" href="https://dribbble.com">
                
                <i class="fab fa-dribbble"></i>
                
            </a>
            
            <a class="level-item button is-white is-marginless" target="_blank" rel="noopener"
                title="RSS" href="/">
                
                <i class="fas fa-rss"></i>
                
            </a>
            
        </div>
        
    </div>
</div>
    
        
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            链接
        </h3>
        <ul class="menu-list">
        
            <li>
                <a class="level is-mobile" href="https://hexo.io" target="_blank" rel="noopener">
                    <span class="level-left">
                        <span class="level-item">Hexo</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">hexo.io</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="https://github.com/ccf19881030" target="_blank" rel="noopener">
                    <span class="level-left">
                        <span class="level-item">github</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">github.com</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="https://blog.csdn.net/ccf19881030" target="_blank" rel="noopener">
                    <span class="level-left">
                        <span class="level-item">csdn</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">blog.csdn.net</span>
                    </span>
                </a>
            </li>
        
            <li>
                <a class="level is-mobile" href="http://havealex.cn" target="_blank" rel="noopener">
                    <span class="level-left">
                        <span class="level-item">个人网站</span>
                    </span>
                    <span class="level-right">
                        <span class="level-item tag">havealex.cn</span>
                    </span>
                </a>
            </li>
        
        </ul>
        </div>
    </div>
</div>

    
        
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                分类
            </h3>
            <ul class="menu-list">
            
            </ul>
        </div>
    </div>
</div>
    
        
    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
            <div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            最新文章
        </h3>
        
        <article class="media">
            
            <a href="/2020/03/04/%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1Serial-Port%E7%B1%BBC-%E5%AE%9E%E7%8E%B0/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="串口通信Serial-Port类C-实现">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-04T09:44:30.915Z">2020-03-04</time></div>
                    <a href="/2020/03/04/%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1Serial-Port%E7%B1%BBC-%E5%AE%9E%E7%8E%B0/" class="title has-link-black-ter is-size-6 has-text-weight-normal">串口通信Serial-Port类C-实现</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/04/Web%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="Web开发技术">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-04T09:39:50.731Z">2020-03-04</time></div>
                    <a href="/2020/03/04/Web%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF/" class="title has-link-black-ter is-size-6 has-text-weight-normal">Web开发技术</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll-1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="如何在Python中使用Linux-epoll">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-02T07:25:13.000Z">2020-03-02</time></div>
                    <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll-1/" class="title has-link-black-ter is-size-6 has-text-weight-normal">如何在Python中使用Linux-epoll</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="如何在Python中使用Linux-epoll">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-02T07:24:16.869Z">2020-03-02</time></div>
                    <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll/" class="title has-link-black-ter is-size-6 has-text-weight-normal">如何在Python中使用Linux-epoll</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/02/21/QT%E5%BC%80%E6%BA%90%E7%BD%91%E7%AB%99%E5%92%8C%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="QT开源网站和相关资料">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-02-21T02:32:34.118Z">2020-02-21</time></div>
                    <a href="/2020/02/21/QT%E5%BC%80%E6%BA%90%E7%BD%91%E7%AB%99%E5%92%8C%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/" class="title has-link-black-ter is-size-6 has-text-weight-normal">QT开源网站和相关资料</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
    </div>
</div>
        
            <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
        
            <div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                标签
            </h3>
            <div class="field is-grouped is-grouped-multiline">
                
            </div>
        </div>
    </div>
</div>
        
        </div>
    
</div>

                




<div class="column is-4-tablet is-4-desktop is-3-widescreen is-hidden-touch is-hidden-desktop-only has-order-3 column-right ">
    
        <div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            最新文章
        </h3>
        
        <article class="media">
            
            <a href="/2020/03/04/%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1Serial-Port%E7%B1%BBC-%E5%AE%9E%E7%8E%B0/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="串口通信Serial-Port类C-实现">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-04T09:44:30.915Z">2020-03-04</time></div>
                    <a href="/2020/03/04/%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1Serial-Port%E7%B1%BBC-%E5%AE%9E%E7%8E%B0/" class="title has-link-black-ter is-size-6 has-text-weight-normal">串口通信Serial-Port类C-实现</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/04/Web%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="Web开发技术">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-04T09:39:50.731Z">2020-03-04</time></div>
                    <a href="/2020/03/04/Web%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF/" class="title has-link-black-ter is-size-6 has-text-weight-normal">Web开发技术</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll-1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="如何在Python中使用Linux-epoll">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-02T07:25:13.000Z">2020-03-02</time></div>
                    <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll-1/" class="title has-link-black-ter is-size-6 has-text-weight-normal">如何在Python中使用Linux-epoll</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="如何在Python中使用Linux-epoll">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-03-02T07:24:16.869Z">2020-03-02</time></div>
                    <a href="/2020/03/02/%E5%A6%82%E4%BD%95%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8Linux-epoll/" class="title has-link-black-ter is-size-6 has-text-weight-normal">如何在Python中使用Linux-epoll</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/02/21/QT%E5%BC%80%E6%BA%90%E7%BD%91%E7%AB%99%E5%92%8C%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="QT开源网站和相关资料">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-02-21T02:32:34.118Z">2020-02-21</time></div>
                    <a href="/2020/02/21/QT%E5%BC%80%E6%BA%90%E7%BD%91%E7%AB%99%E5%92%8C%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/" class="title has-link-black-ter is-size-6 has-text-weight-normal">QT开源网站和相关资料</a>
                    <p class="is-size-7 is-uppercase">
                        
                    </p>
                </div>
            </div>
        </article>
        
    </div>
</div>
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">3</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                标签
            </h3>
            <div class="field is-grouped is-grouped-multiline">
                
            </div>
        </div>
    </div>
</div>
    
    
</div>

            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="如何在Python中使用Linux-epoll" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 ccf19881030&nbsp;
                Powered by <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a> & <a
                        href="https://github.com/ppoffice/hexo-theme-icarus" target="_blank" rel="noopener">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                共<span id="busuanzi_value_site_uv">0</span>个访客
                </span>
		<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
		<span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" rel="noopener" title="Creative Commons" href="https://creativecommons.org/">
                        
                        <i class="fab fa-creative-commons"></i>
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" rel="noopener" title="Attribution 4.0 International" href="https://creativecommons.org/licenses/by/4.0/">
                        
                        <i class="fab fa-creative-commons-by"></i>
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" rel="noopener" title="Download on GitHub" href="https://github.com/ppoffice/hexo-theme-icarus">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
</footer>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("zh-CN");</script>


<script>
var IcarusThemeSettings = {
    site: {
        url: 'http://havealex.cn',
        external_link: {"enable":true,"exclude":[]}
    },
    article: {
        highlight: {
            clipboard: true,
            fold: 'unfolded'
        }
    }
};
</script>


<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>





<script src="/js/animation.js"></script>



<script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
<script src="/js/gallery.js" defer></script>



<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>


<a id="back-to-top" title="回到顶端" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>














<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="想要查找什么..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: '文章',
                PAGES: '页面',
                CATEGORIES: '分类',
                TAGS: '标签',
                UNTITLED: '(无标题)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
	<!-- 雪花特效 -->
	<script type="text/javascript">
		var windowwidth = $(window).width();
		if (windowwidth > 480 ) {
			document.write('<script type="text/javascript" src="js/src/snow2.js"><\/script>');
		}
	</script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
